package com.central.apps.handler;

import cn.hutool.core.map.MapUtil;
import com.central.app.service.HandlerService;
import com.central.apps.handler.base.LvBaseHandler;
import com.central.bizcommon.model.flow.FlowNode;
import com.central.bizcommon.model.flow.FlowOpinion;
import com.central.common.auth.details.LoginAppUser;
import com.central.common.util.SysUserUtil;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Component
public class MaintenanceworkorderHandler extends LvBaseHandler {
    @Autowired
    private HandlerService handlerService;

    /**
     * 检修工作票记录审批后时间及人员
     * @param app
     * @param id
     * @param flowNode
     * @param op
     * @param event
     * @throws Exception
     */
    @Override
    public void afterExcuteFlow(String app, Long id, FlowNode flowNode, FlowOpinion op, String event) throws Exception {
        List<Map<String, Object>> defectreport = handlerService.findBySql("select permituser from bl_maintenanceworkorder where id=" + id);
        Date date = new Date();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String dateString = dateFormat.format(date);
        LoginAppUser loginAppUser = SysUserUtil.getLoginAppUser();
        Map<String, String> map = new HashMap<>();
        map.put("qianf"," issue = '" + loginAppUser.getUsername() + "', issuedate = '" + dateString + "'");
        map.put("xuk"," territorypermission = '" + loginAppUser.getUsername() + "', territorypermissiondate = '" + dateString + "'");
        map.put("zhix"," guardian = '" + loginAppUser.getUsername() + "', signaturedate = '" + dateString + "'");
        //需要判断是提交人还是许可人才能修改参数
        //会签 如果当前人等于终结选定的人那就是终结人 反之就是流程发起人
        if(defectreport.get(0).get("permituser").equals(loginAppUser.getUsername())){
            //最后的许可人
            map.put("zhongj"," territoryconfirmer = '" + loginAppUser.getUsername() + "', territoryconfirmerdate = '" + dateString + "'");
        }else {
            //这是流程发起人
            map.put("zhongj", "  mainconfirmation = mainpersoncharge, mainconfirmationdate = '" + dateString + "'");
        }
        if(StringUtils.isNotBlank(map.get(flowNode.getId()))) {
            handlerService.updateBySql("update bl_maintenanceworkorder set " + map.get(flowNode.getId()) + " where id = " + id);
        }
    }

    /**
     * 检修工作票设置节点自定义审批人
     * @param app
     * @param id
     * @param flowNode
     * @param vars
     * @return
     * @throws Exception
     */
    public String addFlowCandidate(String app, Long id, FlowNode flowNode, Map<String, Object> vars) throws Exception {
        String executer = null;
        Map<String, String> map = new HashMap<>();
        map.put("qianf","designatedduser");
        map.put("xuk","permituser");
        map.put("zhix","workguardian");
        map.put("zhongj","permituser");//他和许可人一样是同一个人
        if(StringUtils.isNotBlank(map.get(flowNode.getId()))){
            //拿到deptcode
            List<Map<String, Object>> defectreport = handlerService.findBySql("select designatedduser,permituser,workguardian,mainpersoncharge from bl_maintenanceworkorder where id=" + id);
            executer = MapUtil.getStr(defectreport.get(0),map.get(flowNode.getId()));
            if(flowNode.getId().equals("zhongj")){
                //会签 选的是检修人就是流程发起人以及终结人 两个
                executer += executer + "," + MapUtil.getStr(defectreport.get(0),"mainpersoncharge");
            }
        }
        return executer;
    }
}
